package com.gdpa.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpa.entity.BrokerBuilding;
import com.gdpa.entity.BrokerBuildingWithAreaName;
import com.gdpa.entity.SaleArea;
import com.gdpa.entity.vo.ResponseData;
import com.gdpa.service.IBrokerBuildingService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 经纪楼盘表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/broker-building")
public class BrokerBuildingController {
    @Autowired
    private IBrokerBuildingService iBrokerBuildingService;

    @RequestMapping("/getBuilding")
    public ResponseData getBuilding(Page page, @Param("search")String search){
        List<BrokerBuildingWithAreaName> buildingList = iBrokerBuildingService.getBuilding(page,search);
        return ResponseData.okPage(page.getTotal(),buildingList);
    }

    @RequestMapping("/add")
    public ResponseData add(BrokerBuildingWithAreaName brokerBuildingWithAreaName){
        boolean isSuccess = false;
        try {
            isSuccess = iBrokerBuildingService.add(brokerBuildingWithAreaName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(isSuccess)
                return ResponseData.okMsg("添加成功!");
            return ResponseData.failMsg("添加失败");
        }

    }

    @RequestMapping("/deleteById/{id}")
    public ResponseData deleteById(@PathVariable("id")Integer id){
        boolean isSuccess = false;
        try {
            isSuccess = iBrokerBuildingService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(isSuccess)
            return ResponseData.okMsg("删除成功!");
        return ResponseData.failMsg("删除失败");
    }

    @RequestMapping("update")
    public ResponseData update(BrokerBuilding brokerBuilding){
        boolean isSuccess = iBrokerBuildingService.update(brokerBuilding);
        if(isSuccess)
            return ResponseData.okMsg("更新成功!");
        return ResponseData.failMsg("更新失败");
    }

    @RequestMapping("/getSaleArea")
    public ResponseData getSaleArea(){
        List<SaleArea> list = iBrokerBuildingService.getSaleArea();
        return ResponseData.okData(list);
    }

    @RequestMapping("/getAreaId")
    public ResponseData getAreaId(Integer id){
        BrokerBuildingWithAreaName brokerBuildingWithAreaName = iBrokerBuildingService.getAreaId(id);
        return ResponseData.okData(brokerBuildingWithAreaName);
    }

    @RequestMapping("/getBuildingNameAndId")
    public ResponseData getBuildingNameAndId(){
        List<BrokerBuildingWithAreaName> list = iBrokerBuildingService.getBuildingNameAndId();
        return ResponseData.okData(list);
    }
}
